package com.jf3q.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.admin.service.SubjectService;
import com.jf3q.admin.service.impl.CourseDescriptionServiceImpl;
import com.jf3q.admin.service.impl.CourseEntityService;
import com.jf3q.admin.service.impl.CourseServiceImpl;
import com.jf3q.admin.utils.vo.AjaxResultVO;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduCourseDescription;
import com.jf3q.common.elatic.entity.CourseEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author ASUS
 * @date 2024/4/16
 * @apiNote
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseServiceImpl courseService;
    @Autowired
    CourseDescriptionServiceImpl courseDescriptionService;
    @Autowired
    SubjectService subjectService;

    @PostMapping("/page")
    public AjaxResultVO page(@RequestBody EduCourse course,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = ""+Integer.MAX_VALUE) Integer pageSize){
        LambdaQueryWrapper<EduCourse> wrapper=new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(course)){
            if (!ObjectUtils.isEmpty(course.getTitle())){
                wrapper.like(EduCourse::getTitle,course.getTitle());
            }
            if (!ObjectUtils.isEmpty(course.getStatus())){
                wrapper.eq(EduCourse::getStatus,course.getStatus());
            }
        }
        wrapper.orderByDesc(EduCourse::getEduModified);
        Page<EduCourse> page=courseService.page(new Page<>(pageNum,pageSize),wrapper);
        return AjaxResultVO.success(page);
    }

    @GetMapping("/getById/{id}")
    public AjaxResultVO toUpdate(@PathVariable String id){
        return AjaxResultVO.success(courseService.getById(id));
    }

    @PostMapping("/saveOrUpdate")
    public AjaxResultVO saveOrUpdate(@RequestBody EduCourse course){
        courseService.saveOrUpdate(course);
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(course.getId());
        courseDescription.setDescription(course.getDescription());
        if (!ObjectUtils.isEmpty(course.getId())){
            //发布了的课程修改基本信息的时候更新一下es
            CourseEntity courseEntity= new CourseEntity();
            EduCourse byId = courseService.getById(course.getId());
            BeanUtils.copyProperties(byId,courseEntity);
            courseEntity.setDescription(courseDescriptionService.getById(course.getId()).getDescription());
            courseEntity.setSubjectName(subjectService.getById(course.getSubjectId()).getTitle());
            courseEntity.setSubjectParentName(subjectService.getById(course.getSubjectParentId()).getTitle());
            courseEntityService.addCourse(courseEntity);
        }


        return courseDescriptionService.saveOrUpdate(courseDescription)?AjaxResultVO.success():AjaxResultVO.error();
    }

    @Autowired
    CourseEntityService courseEntityService;

    @GetMapping("/delete/{id}")
    public AjaxResultVO delete(@PathVariable String id){
        courseEntityService.delCourse(id);
        return courseService.removeById(id)?AjaxResultVO.success():AjaxResultVO.error();
    }

    @GetMapping("/publish/{id}")
    public AjaxResultVO publish(@PathVariable String id){
        EduCourse course = courseService.getById(id);
        if(!course.getStatus().equals("Normal")){//首次发布
            //新增一下es
            CourseEntity courseEntity= new CourseEntity();
            BeanUtils.copyProperties(course,courseEntity);
            courseEntity.setDescription(courseDescriptionService.getById(course.getId()).getDescription());
            courseEntity.setSubjectName(subjectService.getById(course.getSubjectId()).getTitle());
            courseEntity.setSubjectParentName(subjectService.getById(course.getSubjectParentId()).getTitle());
            courseEntityService.addCourse(courseEntity);
        }

        course.setStatus("Normal");
        return courseService.updateById(course)?AjaxResultVO.success():AjaxResultVO.error();
    }
}
